import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.Image;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.DriverManager;

import javax.swing.BorderFactory;
import javax.swing.ButtonModel;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.ScrollPaneConstants;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableColumn;


public class AccountPanel extends JPanel{
	JPanel panel;
	JPanel north, center, south;
	JTable tuitionT, feesT, financeT;
	String[] tuitionCols={"Section","Course Title","Amount","Billiing Credits","CEUs","Status"};
	Object[][] tuitionDatas={};
	String[] feesCols={"Description","Amount"};
	Object[][] feesDatas={	};
	String[] financeCols={"Award","Awarded","Term","Disbursed","Currently Ineligible"};
	Object[][] financeDatas={};
	Image img;
	JLabel name=new JLabel(MenuPanel.fname+" "+MenuPanel.lname);
	JLabel studentId=new JLabel(MenuPanel.student_id);
	JLabel total=new JLabel("Total");
	JLabel totalBalance=new JLabel();
	JLabel payment=new JLabel();
	JLabel dueDate=new JLabel("Due Date");
	JLabel date=new JLabel("10/12/2014");
	float balance;
	
	public AccountPanel(){
		//entire interface
		
		img=new ImageIcon("account_background.png").getImage();
		
		panel=new JPanel();
		panel.setBackground(Color.WHITE);
		panel.setPreferredSize(new Dimension(600,400));
		panel.setLayout(new BorderLayout());
		setOpaque(false);
		panel.setOpaque(false);
		
		//JScrollPane scroll=new JScrollPane(panel);//,JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
		//scroll.setMinimumSize(new Dimension(600,400));
		//scroll.setPreferredSize(new Dimension(600,400));
		
		//initialize the variables by connecting database!
		initialize();
		
		
		///////////////////////////////north
		north=new JPanel();
		north.setBackground(Color.WHITE);
		GridLayout g=new GridLayout(4,2);
		//g.setHgap(5);
		north.setLayout(g);
		
		
		
		north.add(name);
		north.add(studentId);
		north.add(total);
		//north.add(totalBalance);
		north.add(payment);
		north.add(dueDate);
		north.add(date);
		
		north.setOpaque(false);
		panel.add(north,BorderLayout.NORTH);
		
		
		///////////////////////////////////Center
		center=new JPanel();
		center.setBackground(Color.WHITE);
		center.setLayout(new BorderLayout());
		JLabel detail=new JLabel("Account Activity Details");
		detail.setBorder(BorderFactory.createLineBorder((Color.black)));
		center.add(detail,BorderLayout.NORTH);
		//accountDetail
		JPanel accountDetail=new JPanel();
		accountDetail.setBackground(Color.WHITE);
		accountDetail.setLayout(new GridLayout(3,1));
		//accountDetail.setLayout(new BoxLayout(accountDetail, BoxLayout.Y_AXIS));
		
		//tuition
		JPanel tuition=new JPanel();
		tuition.setBackground(Color.WHITE);
		tuition.setLayout(new BorderLayout());
		JLabel tlabel=new JLabel("Tuition by Section");
		tuition.add(tlabel,BorderLayout.NORTH);
		//tcenter
		JPanel tcenter=new JPanel();
		tcenter.setBackground(Color.WHITE);
		//tuitionT=new JTable(tuitionDatas, tuitionCols);	
		
		int v = ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS; 
		int h = ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED;
		tuitionT.setPreferredScrollableViewportSize(new Dimension(580, 38));
		JScrollPane j = new JScrollPane(tuitionT,v,h);
		tuitionT.setFillsViewportHeight(true);
		
		TableColumn column;
		column = tuitionT.getColumnModel().getColumn(0);
		column.setPreferredWidth(100);
		column = tuitionT.getColumnModel().getColumn(1);
		column.setPreferredWidth(200);
		tcenter.add(tuitionT.getTableHeader(), BorderLayout.PAGE_START);
		tcenter.add(j, BorderLayout.CENTER);
		tuition.add(tcenter,BorderLayout.CENTER);
		
		//fees
		JPanel fees=new JPanel();
		fees.setBackground(Color.WHITE);
		fees.setLayout(new BorderLayout());
		JLabel flabel=new JLabel("Fees");
		fees.add(flabel,BorderLayout.NORTH);
		//fcenter
		JPanel fcenter=new JPanel();
		fcenter.setBackground(Color.WHITE);
		
		int v2 = ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS; 
		int h2 = ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED;
		feesT.setPreferredScrollableViewportSize(new Dimension(580, 38));
		JScrollPane j2 = new JScrollPane(feesT,v,h);
		feesT.setFillsViewportHeight(true);
		//feesT=new JTable(feesDatas,feesCols);
		column=feesT.getColumnModel().getColumn(0);
		column.setPreferredWidth(500);
		column=feesT.getColumnModel().getColumn(1);
		column.setPreferredWidth(100);
		fcenter.add(feesT.getTableHeader(), BorderLayout.PAGE_START);
		fcenter.add(j2, BorderLayout.CENTER);
		fees.add(fcenter,BorderLayout.CENTER);
		
		
		//finance
		JPanel finance=new JPanel();
		finance.setBackground(Color.white);
		finance.setLayout(new BorderLayout());
		JLabel filabel=new JLabel("Finance Aid");
		finance.add(filabel,BorderLayout.NORTH);
		//fcenter
		JPanel ficenter=new JPanel();
		//ficenter.setPreferredSize(new Dimension(600,100));
		ficenter.setBackground(Color.white);
		//financeT=new JTable(financeDatas,financeCols);
		int v3 = ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS; 
		int h3 = ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED;
		financeT.setPreferredScrollableViewportSize(new Dimension(580, 38));
		JScrollPane j3 = new JScrollPane(financeT,v,h);
		financeT.setFillsViewportHeight(true);
		//financeT.setSize(new Dimension(600,financeT.getHeight()));
		column=financeT.getColumnModel().getColumn(0);
		column.setPreferredWidth(180);
		column=financeT.getColumnModel().getColumn(1);
		column.setPreferredWidth(100);
		column=financeT.getColumnModel().getColumn(2);
		column.setPreferredWidth(150);
		column=financeT.getColumnModel().getColumn(3);
		column.setPreferredWidth(100);
		ficenter.add(financeT.getTableHeader(), BorderLayout.PAGE_START);
		ficenter.add(j3, BorderLayout.CENTER);
		finance.add(ficenter,BorderLayout.CENTER);
		
		accountDetail.add(tuition);
		accountDetail.add(fees);
		accountDetail.add(finance);
		center.add(accountDetail,BorderLayout.CENTER);
		
		//JScrollPane scroll=new JScrollPane(center);//,JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
		//scroll.setMinimumSize(new Dimension(600,400));
		//scroll.setPreferredSize(new Dimension(600,400));
		
		panel.add(center,BorderLayout.CENTER);
		
		
		/////////////////////////////South
		south=new JPanel();
		south.setBackground(Color.WHITE);
		south.setLayout(new BorderLayout());
		//nsouth
		JPanel nsouth=new JPanel();
		nsouth.setBackground(Color.WHITE);
		JLabel balancelabel=new JLabel("Balance");
		nsouth.setLayout(new GridLayout(1,2));
		nsouth.add(balancelabel);
		nsouth.add(totalBalance);
		south.add(nsouth,BorderLayout.NORTH);
		//JLabel empty=new JLabel("    ");
		
		south.setOpaque(false);
		JPanel buttonPanel=new JPanel();
		buttonPanel.setOpaque(false);
		buttonPanel.setBackground(Color.white);
		buttonPanel.setLayout(new GridBagLayout());
		GridBagConstraints c = new GridBagConstraints();
		final JButton button1=new JButton();
		final ImageIcon ho=new ImageIcon("home_button.png");
		final ImageIcon cho=new ImageIcon("home_cbutton.png");
		button(button1,ho);
		//button = new JButton("5");
		c.fill = GridBagConstraints.HORIZONTAL;
		c.ipady = 0;       //reset to default
		c.weighty = 1.0;   //request any extra vertical space
		c.anchor = GridBagConstraints.PAGE_END; //bottom of space
		c.insets = new Insets(10,0,0,0);  //top padding
		//c.gridx = 1;       //aligned with button 2
		
		button1.addActionListener(new ActionListener(){
	        public void actionPerformed(ActionEvent e){
	              remove(panel);
	              add(new MenuPanel());
	              validate();
	              repaint();
	          }
		});
		buttonPanel.add(button1,c);
		button1.setContentAreaFilled(false);
		button1.setIcon(ho);
		button1.setRolloverIcon(cho);
		button1.setPressedIcon(cho);
		button1.getModel().addChangeListener(new ChangeListener() {
		        @Override
		        public void stateChanged(ChangeEvent e) {
		            ButtonModel model = (ButtonModel) e.getSource();
		            if (model.isRollover()) {
		            	button1.setIcon(cho);
		            } else {
		            	button1.setIcon(ho);
		            }// etc ...//  if (model.isPressed()) {
		         }
		    });
		//whenever return to the main page, and click this page again, it's starting position
		//differs.(slightly goes down)-> need to fix it!!
		south.add(buttonPanel,BorderLayout.CENTER);
		
		panel.add(south,BorderLayout.SOUTH);
		
		
		
	
		//add(scroll);
		//setSize(500,100);
		//setVisible(true);
		
		
		//JScrollPane scroll=new JScrollPane(panel);//,JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
		//scroll.setMinimumSize(new Dimension(600,400));
		//scroll.setPreferredSize(new Dimension(600,400));
		//add(scroll);
		//fix loc error
		
		//make the panel to be located in the middle with graphics
		//JPanel backPanel=new JPanel();
		//backPanel.setPreferredSize(new Dimension(600,400));
		//backPanel.setLayout(new GridBagLayout());
		//backPanel.add(panel);
		
		setLayout(new BorderLayout());
		//add(backPanel,BorderLayout.PAGE_START);
		add(panel,BorderLayout.PAGE_START);
	}
	
	private void initialize(){
		//initialize table/////////
		
		//tuitionT
		DefaultTableModel dtm=new DefaultTableModel(tuitionDatas,tuitionCols){
			public boolean isCellEditable(int nRow, int nCol) {
                return false;
            }
		};
		tuitionT=new JTable(dtm);
		String section,title,ceu="",status="New";
		float amount=0,totalTuition=0;
		int credits;
		float perCredit=500;
		
		
		try{
			String dName = "com.mysql.jdbc.Driver";
			String url = "jdbc:mysql://db4free.net:3306/studentreg14?user=studentreg14&password=KCTSkcts";
			Connection con = DriverManager.getConnection(url);
			java.sql.Statement mysqlStatement = con.createStatement();
			java.sql.ResultSet resultSet = mysqlStatement.executeQuery("SELECT course_name, credit_hours, course_number FROM course_catalog WHERE CID IN (SELECT CID FROM current_course_total_list WHERE SID='"+MenuPanel.SID+"')");
			
			while (resultSet.next()) {
				title=resultSet.getString(1);
				credits=resultSet.getInt(2);
				section=resultSet.getString(3);
				//getSection=course_number+course_section
				amount=(credits*perCredit);
				totalTuition+=amount;
				Object[] tuition={section,title,amount,credits,ceu,status};
				dtm.addRow(tuition);
			}
			con.close();
			}
			catch(Exception ex){}
		
		
		
		//fee data!!
		DefaultTableModel dtm2=new DefaultTableModel(feesDatas,feesCols){
			public boolean isCellEditable(int nRow, int nCol) {
                return false;
            }
		};
		feesT=new JTable(dtm2);
		String description;
		float feeAmount=0,totalFees=0;
		
		try{
			String dName = "com.mysql.jdbc.Driver";
			String url = "jdbc:mysql://db4free.net:3306/studentreg14?user=studentreg14&password=KCTSkcts";
			Connection con = DriverManager.getConnection(url);
			java.sql.Statement mysqlStatement = con.createStatement();
			java.sql.ResultSet resultSet = mysqlStatement.executeQuery("SELECT description, amount FROM course_fee_list WHERE CID IN (SELECT CID FROM current_course_total_list WHERE SID='"+MenuPanel.SID+"')");
			
			while (resultSet.next()) {
				description=resultSet.getString(1);
				feeAmount=resultSet.getInt(2);
				totalFees+=feeAmount;
				Object[] fee={description,feeAmount};
				dtm2.addRow(fee);
			}
			con.close();
			}
			catch(Exception ex){}
		
		
		//financial aid!!!
		DefaultTableModel dtm3=new DefaultTableModel(financeDatas,financeCols){
			public boolean isCellEditable(int nRow, int nCol) {
                return false;
            }
		};
		financeT=new JTable(dtm3);
		String award_name,course_term;
		float financeAmount,disbursed,totalFinance=0;
		
		try{
			String dName = "com.mysql.jdbc.Driver";
			String url = "jdbc:mysql://db4free.net:3306/studentreg14?user=studentreg14&password=KCTSkcts";
			Connection con = DriverManager.getConnection(url);
			java.sql.Statement mysqlStatement = con.createStatement();
			java.sql.ResultSet resultSet = mysqlStatement.executeQuery("SELECT award_name,amount, current_term FROM financial_aid_list WHERE SID ='"+MenuPanel.SID+"'");
			
			while (resultSet.next()) {
				award_name=resultSet.getString(1);
				financeAmount=resultSet.getFloat(2);
				course_term=resultSet.getString(3);
				//disbursed=resultSet.getFloat(4);
				totalFinance+=financeAmount;
				disbursed=(amount+feeAmount);
				Object[] finance={award_name, financeAmount,course_term,disbursed,financeAmount-disbursed};
				dtm3.addRow(finance);
			}
			con.close();
			}
			catch(Exception ex){}
		
		//initialize other values
		balance=totalTuition+totalFees-totalFinance;
		totalBalance.setText("$ "+balance);
		payment.setText("$ "+balance);
	}
	

	@Override
    public void paintComponent(Graphics g) {
		super.paintComponents(g);
        g.drawImage(img, 0, 0, this);
    }
	
	private void button(JButton b, ImageIcon i){
		b.setBorderPainted(false);
		b.setBorder(null);
		//button.setFocusable(false);
		b.setMargin(new Insets(0, 0, 0, 0));
		b.setContentAreaFilled(false);
		b.setIcon(i);
		//b.setRolloverIcon(myIcon2);
		//b.setPressedIcon(myIcon3);
	}
		
}
